package jzqb.controller;

import haha.life.common.BelongToSqlUtils;
import haha.life.common.CutPageBean;
import haha.life.common.PermissionTypeTool;
import haha.life.common.SysTool;
import haha.life.normal.FileuploadUtils;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jzqb.bo.QBBKBo;
import jzqb.bo.StandKnowleageBo;
import jzqb.common.Result;
import jzqb.po.AuthorityGroupPo;
import jzqb.po.QBBKPo;
import jzqb.po.QBBKTemplatePo;
import jzqb.service.QBBKService;
import jzqb.vo.QBBKVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Api(tags = "情报百科")
@RestController
@RequestMapping("/qbbk")
public class QBBKController {

    @Autowired
    private QBBKService qbbkService;

    //封面储存临时路径
    @Value("${filePath.coverTemPResource}")
    private String coverTemPResource;
    //封面储存正式路径
    @Value("${filePath.qbbkCoverPath}")
    private String qbbkCoverPath;

    /*---------------------我的词条--------------------------------*/

    /**
     * 展示 我的词条,百科词条,词条审核
     */
    @ApiOperation(value = "展示情报百科")
    @PostMapping("/queryQBBK")
    public Result queryQBBK(@RequestBody QBBKBo qbbkBo, HttpServletRequest request) {
        List<String> dataSourcesList = qbbkBo.getDataSourcesArr();
        String[] dataSources = dataSourcesList.toArray(new String[dataSourcesList.size()]);
        String belongToDataSources = BelongToSqlUtils.getBelongToSql(dataSources, "领域");
        qbbkBo.setBelongToDataSources(belongToDataSources);//belongTo领域语句
        qbbkBo.setRecorder(TokenUtils.getCurrentUserId(request));//录入人
        qbbkBo.setStart(qbbkBo.getPage(), qbbkBo.getPageSize());//起始条数
        CutPageBean<QBBKVo> cutPage = qbbkService.showQBBK(qbbkBo);
        return Result.succ(cutPage);
    }

    /**
     * 展示一条情报百科
     */
    @ApiOperation(value = "展示一条情报百科")
    @PostMapping("/showSingleQBBK")
    public Result showSingleQBBK(@RequestBody QBBKPo qbbkPo) {
        QBBKVo qbbkVo = qbbkService.showSingleQBBK(qbbkPo);
        return Result.succ(qbbkVo);
    }

    /**
     * 新建情报百科
     */
    @PostMapping("/addQBBK")
    public Result addQBBK(@RequestBody QBBKPo qbbkPo, HttpServletRequest request) throws UnsupportedEncodingException {
        String currentUserId = TokenUtils.getCurrentUserId(request);
        qbbkPo.setRecorder(currentUserId);
        qbbkPo.setAuthor(currentUserId);
        Boolean bl = qbbkService.addQBBK(qbbkPo);
        if (bl) {
            return Result.succ("添加成功!");
        } else {
            return Result.fail("添加失败!");
        }
    }


    @ApiOperation(value = "修改情报百科")
    @PostMapping("/updateQBBK")
    public Result updateQBBK(@RequestBody QBBKPo qbbkPo) throws UnsupportedEncodingException {
        //审核意见置空
        qbbkPo.setAuditAdvice("");
        Boolean bl = qbbkService.updateQBBK(qbbkPo);
        if (bl) {
            return Result.succ("修改成功!");
        } else {
            return Result.fail("修改失败!");
        }
    }


    /**
     * 情报百科退审
     */
    @PostMapping("/recallQBBK")
    public Result recallQBBK(@RequestBody QBBKPo qbbkPo) {
        qbbkPo.setAuditAdvice("被退审");
        Boolean bl = qbbkService.recallQBBK(qbbkPo);
        if (bl) {
            return Result.succ("退审成功!");
        } else {
            return Result.fail("退审失败!");
        }
    }

    /**
     * 词条审核-管理员进行词条审核操作,改变词条状态
     */
    @ApiOperation(value = "词条审核操作(过审和驳回)")
    @PostMapping("/reviewQBBK")
    public Result reviewQBBK(@RequestBody QBBKPo qbbkPo, HttpServletRequest request) {
        qbbkPo.setAuditPerson(TokenUtils.getCurrentUserId(request));
        qbbkPo.setAuditTime(SysTool.getCurrentTime(SysTool.dateFormat_milli));
        Boolean bl = qbbkService.reviewQBBK(qbbkPo);
        return Result.succ(bl);
    }


    @ApiOperation(value = "删除情报百科")
    @PostMapping("/deleteQBBK")
    public Result deleteQBBK(@RequestBody QBBKPo qbbkPo) {
        Boolean bl = qbbkService.deleteQBBK(qbbkPo);
        if (bl) {
            return Result.succ("删除成功!");
        } else {
            return Result.fail("删除失败!");
        }
    }

    @ApiOperation(value = "获取情报百科封面")
    @GetMapping("/getQBBKCover")
    public void getQBBKCover(@RequestParam("cover") String cover, HttpServletResponse response) {
        if ("".equals(cover) || cover == null) {
            return;
        }
        String absolutePath = qbbkCoverPath + "\\" + cover;
        try {
            FileuploadUtils.download(absolutePath, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*------------------------其他--------------------------------*/

    /**
     * 保存封面
     *
     * @param file     上传的文件
     * @param response 请求对象
     * @return 执行结果
     */
    @RequestMapping(value = "/saveCoverOfqbbk", method = {RequestMethod.POST})
    public String uploading(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
//        response.setHeader("Content-Type", "text/html");
//        response.addHeader("Content-Type", "text/html");
        String currentUserId = TokenUtils.getCurrentUserId(request);
        String nowTime = SysTool.getCurrentTime(SysTool.dateFormat_date);
        String basePath = coverTemPResource + "\\" + currentUserId + "\\" + nowTime;
        String jsonStr = qbbkService.uploading(file, basePath);
        return jsonStr;
    }


    /**
     * sysID 查询情报成果详情
     */
    @PostMapping("/getQBBKDetail")
    public Result getResultDetail(@RequestBody QBBKPo qbbkPo) {
        QBBKPo qbbk = qbbkService.getQBBKDetail(qbbkPo);
        return Result.succ(qbbk);
    }

    /**
     * 查询分类领域的数量
     */
    @PostMapping("/getFieldNum")
    public Result getFieldNum(@RequestBody QBBKBo qbbkBo, HttpServletRequest request) {
        if (qbbkBo.getFlag() == 0) {//flag=0 为我的词条
            qbbkBo.setRecorder(TokenUtils.getCurrentUserId(request));
        }
        List<StandKnowleageBo> list = qbbkService.getFieldNumByFlag(qbbkBo);
        return Result.succ(list);
    }

    /**
     * 获取不同状态百科词条的数量:0:文件暂存  1:审核中  2:审核通过   3:审核未通过
     */
    @PostMapping("/getDiffStatusQBBKNum")
    public Result getDiffStatusQBBKNum(@RequestBody QBBKBo qbbkBo, HttpServletRequest request) {
        String currentUserId = TokenUtils.getCurrentUserId(request);
        qbbkBo.setRecorder(currentUserId);
        List<StandKnowleageBo> list = qbbkService.getDiffStatusQBBKNum(qbbkBo);
        return Result.succ(list);
    }

    /**
     * 获取审核列表(新建百科词条时使用)
     */
    @PostMapping("/getAuthorityGroupPersons")
    public Result getAuthorityGroupPersons() {
        //审核组对应的id
        Integer AuditGroupId = Integer.valueOf(PermissionTypeTool.front_businessManageGroupId);
        AuthorityGroupPo auditPersons = qbbkService.getAuditPersons(AuditGroupId);
        return Result.succ(auditPersons);
    }

    /*--------------------------情报百科模板--------------------------------------------*/
    @GetMapping("/showTemplateList")
    public Result showTemplateList() {
        List<QBBKTemplatePo> qbbkTemplatePos = qbbkService.showTemplateList();
        return Result.succ(qbbkTemplatePos);
    }

    @PostMapping("/addQBBKTemplate")
    public Result addQBBKTemplate(@RequestBody QBBKTemplatePo qbbkTemplatePo, HttpServletRequest request) throws UnsupportedEncodingException {
        Object object = qbbkService.addQBBKTemplate(qbbkTemplatePo, request);
        if (SysTool.templateRepeat.equals(object.toString())) {
            return Result.fail(SysTool.templateRepeat);
        }
        return Result.succ(object);
    }

    @PostMapping("/updateQBBKTemplate")
    public Result updateQBBKTemplate(@RequestBody QBBKTemplatePo qbbkTemplatePo) throws UnsupportedEncodingException {
        Object object = qbbkService.updateQBBKTemplate(qbbkTemplatePo);
        if (SysTool.templateRepeat.equals(object.toString())) {
            return Result.fail(SysTool.templateRepeat);
        }
        return Result.succ(object);
    }


    @PostMapping("/delQBBKTemplate")
    public Result delQBBKTemplate(@RequestBody Map<String, Integer> map) {
        Integer id = map.get("id");
        Boolean bl = qbbkService.delQBBKTemplate(id);
        if (bl) {
            return Result.succ("删除成功!");
        } else {
            return Result.fail("删除失败!");
        }
    }
}
